package com.hpin.fansea.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomButtonsController;

import com.hpin.fansea.R;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.utils.LogUtil;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * Created by Aaron on 15/11/3.
 */
public class BunnerDetailActivity extends BaseActivity {


    private WebView wb_detail;
    private Button back_activity_back;
    private String detail;
    private WebSettings settings;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bunner_detail);

        wb_detail = (WebView) findViewById(R.id.wb_detail);

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);

        iv_left.setOnClickListener(this);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("详情");

        detail = getIntent().getStringExtra("detail");

        // //设置双击屏幕 放大或缩小
        // settings.setUseWideViewPort(true);
        // //启动放大缩小控制器
        // settings.setBuiltInZoomControls(true);
        //
        // wb_detail.setWebViewClient(new WebViewClient() {
        // public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // view.loadUrl(url);
        // return true;
        // }
        // });
        //
        // DisplayMetrics metrics = new DisplayMetrics();
        // getWindowManager().getDefaultDisplay().getMetrics(metrics);
        // int mDensity = metrics.densityDpi;
        // if (mDensity <= 120) {
        // settings.setDefaultZoom(ZoomDensity.CLOSE);
        //
        // LogUtil.e("TAG", "mDensity==========>" + mDensity);
        //
        // } else if (120 < mDensity && mDensity <= 160) {
        // LogUtil.e("TAG", "mDensity==========>" + mDensity);
        // settings.setDefaultZoom(ZoomDensity.MEDIUM);
        // } else if (160 < mDensity && mDensity <= 240) {
        // LogUtil.e("TAG", "mDensity==========>" + mDensity);
        // settings.setDefaultZoom(ZoomDensity.FAR);
        // settings.setLoadWithOverviewMode(true);
        // }
        //
        // LogUtil.e("TAG", "other ::::: mDensity==========>" + mDensity);
        //
        //
        // settings.setDefaultZoom(WebSettings.ZoomDensity.FAR);
        // settings.setUseWideViewPort(true);
        // settings.setLoadWithOverviewMode(true);
        // settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //
        // // 显示图片
        // wb_detail.loadUrl(detail);

        iv_left.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();

        setWeb(detail);
    }

    @SuppressLint("NewApi")
    public void setWeb(String url) {


        settings = wb_detail.getSettings();
//		settings.setTextSize(WebSettings.TextSize.NORMAL);


        // 设置默认缩放方式尺寸是far

        // settings.setUseWideViewPort(true);
        // settings.setLoadWithOverviewMode(true);

        // 获得屏幕的宽高
        DisplayMetrics outMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
        int widthPixels = outMetrics.widthPixels; // 屏幕的宽
        int heightPixels = outMetrics.heightPixels; // 屏幕的高

        LogUtil.e("TAG", "屏幕的宽:============>" + widthPixels + "屏幕的高:::====>"
                + heightPixels);

        if (widthPixels > 720) {
            if (heightPixels == 1800) {
                //设置缩放比例
                wb_detail.setInitialScale(170);
            } else {
                loadNormal();
            }
            //设置文字缩放比例
            settings.setTextZoom(420);
        } else {
            //设置缩放比例
            wb_detail.setInitialScale(280);
            //设置文字缩放比例
            settings.setTextZoom(150);
        }

        wb_detail.loadUrl(url);
    }

    @SuppressLint("NewApi")
    private void loadNormal() {

        // 放大缩小
        settings.setBuiltInZoomControls(true);

        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

        // 去掉缩放按钮
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
            // Use the API 11+ calls to disable the controls
            wb_detail.getSettings().setBuiltInZoomControls(true);
            wb_detail.getSettings().setDisplayZoomControls(false);
        } else {
            // Use the reflection magic to make it work on earlier APIs
            getControlls();
        }

        setZoomControlGone(wb_detail);
    }

    // 把webviwe 的放大缩小按钮隐藏掉
    private void getControlls() {
        try {
            Class webview = Class.forName("android.webkit.WebView");
            Method method = webview.getMethod("getZoomButtonsController");
            ZoomButtonsController zoom_controll = (ZoomButtonsController) method
                    .invoke(this, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 把webviwe 的放大缩小按钮隐藏掉
    public void setZoomControlGone(View view) {
        Class classType;
        Field field;
        try {
            classType = WebView.class;
            field = classType.getDeclaredField("mZoomButtonsController");
            field.setAccessible(true);
            ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(
                    view);
            mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
            try {
                field.set(view, mZoomButtonsController);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void widgetClick(View v) {

    }
}
